package com.qclix.inv.webservice;

import com.qclix.inv.Client;
//import com.qclix.inv.InvoiceService;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.springframework.ws.server.endpoint.AbstractJDomPayloadEndpoint;

import org.jdom.xpath.XPath;

/**
 * Endpoint for the newClient web service request
 */
public class NewClientJDomEndPoint extends AbstractJDomPayloadEndpoint {
    
    private XPath clientNameExpression;
    private XPath clientStreet1Expression;
    private XPath clientStreet2Expression;
    private XPath clientZipExpression;
    private XPath clientCityExpression;
    private XPath clientCountryExpression;
    private XPath clientVATExpression;
    Namespace invoiceNamespace = Namespace.getNamespace("inv", "http://www.quattroclix.com/invoice");
    Namespace invoiceWSNamespace = Namespace.getNamespace("inv", "http://www.quattroclix.com/invoice-ws");

    //private InvoiceService invoiceService;

    public NewClientJDomEndPoint() throws JDOMException {
        clientNameExpression = XPath.newInstance("//inv:client/inv:name");
        clientNameExpression.addNamespace(invoiceNamespace);
        clientStreet1Expression = XPath.newInstance("//inv:client/inv:street1");
        clientStreet1Expression.addNamespace(invoiceNamespace);
        clientStreet2Expression = XPath.newInstance("//inv:client/inv:street2");
        clientStreet2Expression.addNamespace(invoiceNamespace);
        clientZipExpression = XPath.newInstance("//inv:client/inv:zip");
        clientZipExpression.addNamespace(invoiceNamespace);
        clientCityExpression = XPath.newInstance("//inv:client/inv:city");
        clientCityExpression.addNamespace(invoiceNamespace);
        clientCountryExpression = XPath.newInstance("//inv:client/inv:country");
        clientCountryExpression.addNamespace(invoiceNamespace);
        clientVATExpression = XPath.newInstance("//inv:client/inv:vat");
        clientVATExpression.addNamespace(invoiceNamespace);
    }

    /*
    public void setInvoiceService(InvoiceService invoiceService) {
        this.invoiceService = invoiceService;
    }
    */

    @Override
    protected org.jdom.Element invokeInternal(org.jdom.Element element) throws Exception {
        Client newClient = new Client();
        newClient.setName(clientNameExpression.valueOf(element));
        newClient.setStreet1(clientStreet1Expression.valueOf(element));
        newClient.setStreet2(clientStreet2Expression.valueOf(element));
        newClient.setZip(clientZipExpression.valueOf(element));
        newClient.setCity(clientCityExpression.valueOf(element));
        newClient.setCountry(clientCountryExpression.valueOf(element));
        newClient.setVat(clientVATExpression.valueOf(element));

        System.out.println("Received request to create client " + newClient);

        // TODO replace this with actual save
        newClient.setId(23L);
        Element exceptionElement = new Element("newClientException", invoiceWSNamespace);
        Element clientIdElement = new Element("errorCode", invoiceWSNamespace);
        clientIdElement.setText("Duplicated client " + newClient.getName());
        exceptionElement.addContent(clientIdElement);
        return exceptionElement;
        /*
        Element responseElement = new Element("newClientResponse", invoiceWSNamespace);
        Element clientIdElement = new Element("clientId", invoiceWSNamespace);
        clientIdElement.setText("" + newClient.getId());
        responseElement.addContent(clientIdElement);
        return responseElement;
        */
    }
}
